<?php

class Follow_model extends CI_Model {

	public $userId;
	public $targetId;
	public $type;
	
	public function followUser($userId, $targetId)	{
		$this->userId = $userId;
		$this->targetId = $targetId;
		$this->type = 1;
		$result = $this->db->insert('follows', $this);
		return $this->db->insert_id();
	}
	
	public function isUserFollowing($userId, $targetId)	{
		$this->db->where('userId', $userId);
		$this->db->where('targetId', $targetId);
		$this->db->where('type', 1);
		$query = $this->db->get('follows', 1);
		$result = $query->result();
		return (count($result) > 0);
	}
	
	public function getFollowers($objectId, $find = "follower", $type=1){
		if($find == "follower"){
			$this->db->where('userId', $objectId);
		}else{
			$this->db->where('targetId', $objectId);
		}
		$this->db->where('type', $type);
		$query = $this->db->get('follows', 100);
		return $query->result();
	}
}

?>